﻿namespace QLPhongKham.Access.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using QLPhongKham.Access;
    using QLPhongKham.BaseClass.BenhNhan;
    using QLPhongKham.BaseClass.Chung;
    using QLPhongKham.BaseClass.Duoc;
    using System;
    using System.Data;

    public class DToaThuoc
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _conn;
        private General _xuli;

        public DToaThuoc()
        {
            this._condata = new ConnectDatabase();
            this._xuli = new General(this._condata);
        }

        public DToaThuoc(ConnectDatabase condata)
        {
            this._condata = condata;
            this._xuli = new General(this._condata);
        }

        private string f_getsql_toathuoc(long idql, string mabn, long idtoathuoc)
        {
            object obj2;
            string str = "";
            if (idql != 0L)
            {
                obj2 = str;
                str = string.Concat(new object[] { obj2, " and a.", dtoDToaThuoc.columnIDQL, "=", idql });
            }
            if (idtoathuoc != 0L)
            {
                obj2 = str;
                str = string.Concat(new object[] { obj2, " and a.", dtoDToaThuoc.columnID, "=", idtoathuoc });
            }
            if (mabn != "")
            {
                string str5 = str;
                str = str5 + " and a." + dtoDToaThuoc.columnMABN + "='" + mabn + "'";
            }
            string str2 = this._condata.Schema + ".";
            return ("select a." + dtoDToaThuoc.columnIDQL + " as idql,a." + dtoDToaThuoc.columnID + " as id,a." + dtoDToaThuoc.columnMABN + " as mabn,b3." + dtoBNThongTinBN.columnHOTEN + " as hoten,to_char(" + dtoDToaThuoc.columnNGAY + ",'dd/mm/yyyy hh24:mi') as ngay,a." + dtoDToaThuoc.columnIDPHONG + " as idphong,a." + dtoDToaThuoc.columnIDDANGNHAP + " as iddangnhap,a." + dtoDToaThuoc.columnCHANDOAN + " as chandoan,b." + dtoDMDangNhap.columnTENNGUOIDUNG + " as tennguoidangnhap,a." + dtoDToaThuoc.columnIDBACSI + " as idbacsi,b2." + dtoBNDMBacSi.columnTEN + " as tenbacsi from " + str2 + dtoDToaThuoc.tableName + " a left join " + str2 + dtoDMDangNhap.tableName + " b on a." + dtoDToaThuoc.columnIDDANGNHAP + "=b." + dtoDMDangNhap.columnID + " left join " + str2 + dtoBNDMBacSi.tableName + " b2 on a." + dtoDToaThuoc.columnIDBACSI + "=b2." + dtoBNDMBacSi.columnID + " left join " + str2 + dtoBNThongTinBN.tableName + " b3 on a." + dtoDToaThuoc.columnMABN + "=b3." + dtoBNThongTinBN.columnMABN + " where " + ((str != "") ? (" 1=1 " + str) : " 1=0") + " order by a." + dtoDToaThuoc.columnNGAY + " desc");
        }

        public bool f_huy_toathuoc(long id)
        {
            int num = -1;
            try
            {
                NpgsqlConnection connect = new NpgsqlConnection(this._condata.StringConnect);
                connect.Open();
                NpgsqlTransaction transaction = connect.BeginTransaction();
                try
                {
                    num = this._xuli.f_Update_dulieu(string.Concat(new object[] { "delete from ", this._xuli.pSchema, ".", dtoDToaThuocCT.tableName, " where ", dtoDToaThuocCT.columnIDTOATHUOC, "=", id }), connect);
                    num = -1;
                    num = this._xuli.f_Update_dulieu(string.Concat(new object[] { "delete from ", this._xuli.pSchema, ".", dtoDToaThuoc.tableName, " where ", dtoDToaThuoc.columnID, "=", id }), connect);
                    if (num > -1)
                    {
                        transaction.Commit();
                    }
                    else
                    {
                        transaction.Rollback();
                    }
                }
                catch
                {
                    transaction.Rollback();
                }
                finally
                {
                    connect.Close();
                    connect.Dispose();
                }
            }
            catch
            {
            }
            return (num > -1);
        }

        public bool f_huy_toathuoc_ngoaivien(long id)
        {
            int num = -1;
            try
            {
                NpgsqlConnection connect = new NpgsqlConnection(this._condata.StringConnect);
                connect.Open();
                NpgsqlTransaction transaction = connect.BeginTransaction();
                try
                {
                    num = this._xuli.f_Update_dulieu(string.Concat(new object[] { "delete from ", this._xuli.pSchema, ".", dtoDToaThuocNgoaiVienCT.tableName, " where ", dtoDToaThuocNgoaiVienCT.columnIDTOATHUOC, "=", id }), connect);
                    num = -1;
                    num = this._xuli.f_Update_dulieu(string.Concat(new object[] { "delete from ", this._xuli.pSchema, ".", dtoDToaThuocNgoaiVien.tableName, " where ", dtoDToaThuocNgoaiVien.columnID, "=", id }), connect);
                    if (num > -1)
                    {
                        transaction.Commit();
                    }
                    else
                    {
                        transaction.Rollback();
                    }
                }
                catch
                {
                    transaction.Rollback();
                }
                finally
                {
                    connect.Close();
                    connect.Dispose();
                }
            }
            catch
            {
            }
            return (num > -1);
        }

        public decimal f_insert_toathuoc(dtoDToaThuoc ToaThuoc, dtoDToaThuocCT[] chitiet)
        {
            decimal num2;
            this._conn = new NpgsqlConnection(this._condata.StringConnect);
            this._conn.Open();
            try
            {
                ToaThuoc.pID = long.Parse(this._xuli.f_Get_CapID());
                if (!ToaThuoc.Insert(this._conn))
                {
                    return 0M;
                }
                NpgsqlTransaction transaction = this._conn.BeginTransaction();
                int index = 0;
                index = 0;
                while (index < chitiet.Length)
                {
                    chitiet[index].pIDTOATHUOC = ToaThuoc.pID;
                    if (chitiet[index].pID != 0L)
                    {
                        if (!chitiet[index].Update(this._conn))
                        {
                            transaction.Rollback();
                            break;
                        }
                    }
                    else
                    {
                        chitiet[index].pID = long.Parse(this._xuli.f_Get_CapID());
                        if (!chitiet[index].Insert(this._conn))
                        {
                            chitiet[index].pID = 0L;
                            transaction.Rollback();
                            break;
                        }
                    }
                    index++;
                }
                if (index == chitiet.Length)
                {
                    transaction.Commit();
                    return ToaThuoc.pID;
                }
                transaction.Rollback();
                ToaThuoc.Delete();
                num2 = 0M;
            }
            catch
            {
                num2 = 0M;
            }
            finally
            {
                this._conn.Close();
                this._conn = null;
            }
            return num2;
        }

        public decimal f_insert_toathuoc_ngoaivien(dtoDToaThuocNgoaiVien ToaThuoc, dtoDToaThuocNgoaiVienCT[] chitiet)
        {
            decimal num2;
            this._conn = new NpgsqlConnection(this._condata.StringConnect);
            this._conn.Open();
            try
            {
                ToaThuoc.pID = long.Parse(this._xuli.f_Get_CapID());
                if (!ToaThuoc.Insert(this._conn))
                {
                    num2 = 0M;
                }
                else
                {
                    try
                    {
                        NpgsqlTransaction transaction = this._conn.BeginTransaction();
                        int index = 0;
                        index = 0;
                        while (index < chitiet.Length)
                        {
                            chitiet[index].pIDTOATHUOC = ToaThuoc.pID;
                            if (chitiet[index].pID != 0L)
                            {
                                if (!chitiet[index].Update(this._conn))
                                {
                                    transaction.Rollback();
                                    break;
                                }
                            }
                            else
                            {
                                chitiet[index].pID = long.Parse(this._xuli.f_Get_CapID());
                                if (!chitiet[index].Insert(this._conn))
                                {
                                    chitiet[index].pID = 0L;
                                    transaction.Rollback();
                                    break;
                                }
                            }
                            index++;
                        }
                        if (index == chitiet.Length)
                        {
                            transaction.Commit();
                            return ToaThuoc.pID;
                        }
                        ToaThuoc.Delete();
                        num2 = 0M;
                    }
                    catch
                    {
                        ToaThuoc.Delete();
                        num2 = 0M;
                    }
                }
            }
            catch
            {
                num2 = 0M;
            }
            finally
            {
                this._conn.Close();
                this._conn = null;
            }
            return num2;
        }

        public DataTable f_loaddata_toathuoc(string mabn)
        {
            DataTable table = new DataTable();
            return this._xuli.f_Get_dulieu(this.f_getsql_toathuoc(0L, mabn, 0L)).Tables[0];
        }

        public DataTable f_loaddata_toathuoc(long idql, long idtoathuoc)
        {
            DataTable table = new DataTable();
            return this._xuli.f_Get_dulieu(this.f_getsql_toathuoc(idql, "", idtoathuoc)).Tables[0];
        }

        public DataTable f_loaddata_toathuoc_ngoaivien(long idql)
        {
            DataTable table = new DataTable();
            string str = this._condata.Schema + ".";
            string sql = string.Concat(new object[] { 
                "select a.", dtoDToaThuocNgoaiVien.columnIDQL, " as idql,a.", dtoDToaThuocNgoaiVien.columnID, " as id,a.", dtoDToaThuocNgoaiVien.columnMABN, " as mabn,b3.", dtoBNThongTinBN.columnHOTEN, " as hoten,to_char(", dtoDToaThuocNgoaiVien.columnNGAY, ",'dd/mm/yyyy hh24:mi') as ngay,a.", dtoDToaThuoc.columnIDPHONG, " as idphong,a.", dtoDToaThuocNgoaiVien.columnIDDANGNHAP, " as iddangnhap,a.", dtoDToaThuocNgoaiVien.columnCHANDOAN, 
                " as chandoan,b.", dtoDMDangNhap.columnTENNGUOIDUNG, " as tennguoidangnhap,a.", dtoDToaThuocNgoaiVien.columnIDBACSI, " as idbacsi,b2.", dtoBNDMBacSi.columnTEN, " as tenbacsi from ", str, dtoDToaThuocNgoaiVien.tableName, " a left join ", str, dtoDMDangNhap.tableName, " b on a.", dtoDToaThuocNgoaiVien.columnIDDANGNHAP, "=b.", dtoDMDangNhap.columnID, 
                " left join ", str, dtoBNDMBacSi.tableName, " b2 on a.", dtoDToaThuocNgoaiVien.columnIDBACSI, "=b2.", dtoBNDMBacSi.columnID, " left join ", str, dtoBNThongTinBN.tableName, " b3 on a.", dtoDToaThuocNgoaiVien.columnMABN, "=b3.", dtoBNThongTinBN.columnMABN, " where a.", dtoDToaThuocNgoaiVien.columnIDQL, 
                "=", idql, " order by a.", dtoDToaThuocNgoaiVien.columnNGAY, " desc"
             });
            return this._xuli.f_Get_dulieu(sql).Tables[0];
        }

        public DataTable f_loaddata_toathuoc_ngoaivienct(long idtoathuoc, long idql, string mabn)
        {
            string str = ((idtoathuoc != 0L) ? string.Concat(new object[] { " and a.", dtoDToaThuoc.columnID, "=", idtoathuoc }) : "") + ((idql != 0L) ? string.Concat(new object[] { " and a.", dtoDToaThuoc.columnIDQL, "=", idql }) : "") + ((mabn != "") ? (" and a." + dtoDToaThuoc.columnMABN + "='" + mabn + "'") : "");
            DataTable table = new DataTable();
            string str2 = this._condata.Schema + ".";
            string sql = "select a." + dtoDToaThuocNgoaiVien.columnIDQL + " as idql,a." + dtoDToaThuocNgoaiVien.columnID + " as id,a." + dtoDToaThuocNgoaiVien.columnMABN + " as mabn,to_char(" + dtoDToaThuocNgoaiVien.columnNGAY + ",'dd/mm/yyyy hh24:mi') as ngay,b2." + dtoDDMDuocPham.columnID + " as idvp,b." + dtoDToaThuocNgoaiVienCT.columnSOLUONG + " as soluong,b." + dtoDToaThuocNgoaiVienCT.columnSOLUONGSANG + " as slsang,b." + dtoDToaThuocNgoaiVienCT.columnSOLUONGTRUA + " as sltrua,b." + dtoDToaThuocNgoaiVienCT.columnSOLUONGCHIEU + " as slchieu,b." + dtoDToaThuocNgoaiVienCT.columnSOLUONGTOI + " as sltoi,b2." + dtoDDMDuocPham.columnMA + " as mavp,b2." + dtoDDMDuocPham.columnTEN + " as tenvp,b2." + dtoDDMDuocPham.columnDVT + " as dvt,b2." + dtoDDMDuocPham.columnTENHOATCHAT + " as tenhoatchat,b." + dtoDToaThuocNgoaiVienCT.columnSONGAY + " as songay,b." + dtoDToaThuocNgoaiVienCT.columnCACHDUNG + " as cachdung from " + str2 + dtoDToaThuocNgoaiVien.tableName + " a inner join " + str2 + dtoDToaThuocNgoaiVienCT.tableName + " b on a." + dtoDToaThuocNgoaiVien.columnID + "=b." + dtoDToaThuocNgoaiVienCT.columnIDTOATHUOC + " inner join " + str2 + dtoDDMDuocPham.tableName + " b2 on b." + dtoDToaThuocNgoaiVienCT.columnIDDP + "=b2." + dtoDDMDuocPham.columnID + " where " + ((str != "") ? (" 1=1 " + str) : " 1=0") + " order by a." + dtoDToaThuocNgoaiVien.columnNGAY + " desc";
            return this._xuli.f_Get_dulieu(sql).Tables[0];
        }

        public DataTable f_loaddata_toathuocct(long idtoathuoc, long idql, string mabn)
        {
            string str = ((idtoathuoc != 0L) ? string.Concat(new object[] { " and a.", dtoDToaThuoc.columnID, "=", idtoathuoc }) : "") + ((idql != 0L) ? string.Concat(new object[] { " and a.", dtoDToaThuoc.columnIDQL, "=", idql }) : "") + ((mabn != "") ? (" and a." + dtoDToaThuoc.columnMABN + "='" + mabn + "'") : "");
            DataTable table = new DataTable();
            string str2 = this._condata.Schema + ".";
            string sql = "select a." + dtoDToaThuoc.columnIDQL + " as idql,a." + dtoDToaThuoc.columnID + " as id,b." + dtoDToaThuocCT.columnID + " as idchitiet,a." + dtoDToaThuoc.columnMABN + " as mabn,to_char(" + dtoDToaThuoc.columnNGAY + ",'dd/mm/yyyy hh24:mi') as ngay,b2." + dtoDDMDuocPham.columnID + " as idvp,b." + dtoDToaThuocCT.columnSOLUONG + " as soluong,b." + dtoDToaThuocCT.columnSOLUONGSANG + " as slsang,b." + dtoDToaThuocCT.columnSOLUONGTRUA + " as sltrua,b." + dtoDToaThuocCT.columnSOLUONGCHIEU + " as slchieu,b." + dtoDToaThuocCT.columnSOLUONGTOI + " as sltoi,b." + dtoDToaThuocCT.columnDONGIA + " as dongia,b." + dtoDToaThuocCT.columnSLDATHANHTOAN + " as sldathanhtoan,b." + dtoDToaThuocCT.columnSOLUONG + "*b." + dtoDToaThuocCT.columnDONGIA + " as thanhtien,b2." + dtoDDMDuocPham.columnMA + " as mavp,b2." + dtoDDMDuocPham.columnTEN + " as tenvp,b2." + dtoDDMDuocPham.columnDVT + " as dvt,b2." + dtoDDMDuocPham.columnTENHOATCHAT + " as tenhoatchat,b." + dtoDToaThuocCT.columnSONGAY + " as songay,b." + dtoDToaThuocCT.columnCACHDUNG + " as cachdung from " + str2 + dtoDToaThuoc.tableName + " a inner join " + str2 + dtoDToaThuocCT.tableName + " b on a." + dtoDToaThuoc.columnID + "=b." + dtoDToaThuocCT.columnIDTOATHUOC + " inner join " + str2 + dtoDDMDuocPham.tableName + " b2 on b." + dtoDToaThuocCT.columnIDDP + "=b2." + dtoDDMDuocPham.columnID + " where " + ((str != "") ? (" 1=1 " + str) : " 1=0") + " order by a." + dtoDToaThuoc.columnNGAY + " desc";
            return this._xuli.f_Get_dulieu(sql).Tables[0];
        }

        public decimal f_update_toathuoc(dtoDToaThuoc ToaThuoc, dtoDToaThuocCT[] chitiet)
        {
            decimal pID;
            this._conn = new NpgsqlConnection(this._condata.StringConnect);
            this._conn.Open();
            NpgsqlTransaction transaction = this._conn.BeginTransaction();
            try
            {
                if (!ToaThuoc.Update(this._conn))
                {
                    transaction.Rollback();
                    return 0M;
                }
                string str = "";
                for (int i = 0; i < chitiet.Length; i++)
                {
                    if (chitiet[i].pID != 0L)
                    {
                        if (!chitiet[i].Update(this._conn))
                        {
                            return 0M;
                        }
                    }
                    else
                    {
                        chitiet[i].pID = long.Parse(this._xuli.f_Get_CapID()) + i;
                        if (!chitiet[i].Insert(this._conn))
                        {
                            return 0M;
                        }
                    }
                    str = str + chitiet[i].pID.ToString() + ",";
                }
                transaction.Commit();
                str = str.Trim(new char[] { ',' });
                if (str != "")
                {
                    this._xuli.f_Update_dulieu(string.Concat(new object[] { "delete from ", this._xuli.pSchema, ".", dtoDToaThuocCT.tableName, " where ", dtoDToaThuocCT.columnID, " not in (", str, ") and ", dtoDToaThuocCT.columnIDTOATHUOC, "=", ToaThuoc.pID }));
                }
                pID = ToaThuoc.pID;
            }
            catch
            {
                transaction.Rollback();
                pID = 0M;
            }
            finally
            {
                this._conn.Close();
                this._conn = null;
            }
            return pID;
        }

        public decimal f_update_toathuoc_ngoaivien(dtoDToaThuocNgoaiVien ToaThuoc, dtoDToaThuocNgoaiVienCT[] chitiet)
        {
            decimal pID;
            this._conn = new NpgsqlConnection(this._condata.StringConnect);
            this._conn.Open();
            NpgsqlTransaction transaction = this._conn.BeginTransaction();
            try
            {
                if (!ToaThuoc.Update(this._conn))
                {
                    transaction.Rollback();
                    return 0M;
                }
                string str = "";
                for (int i = 0; i < chitiet.Length; i++)
                {
                    if (chitiet[i].pID != 0L)
                    {
                        if (!chitiet[i].Update(this._conn))
                        {
                            transaction.Rollback();
                            return 0M;
                        }
                    }
                    else
                    {
                        chitiet[i].pID = long.Parse(this._xuli.f_Get_CapID());
                        if (!chitiet[i].Insert(this._conn))
                        {
                            transaction.Rollback();
                            return 0M;
                        }
                    }
                    str = str + chitiet[i].pID.ToString() + ",";
                }
                transaction.Commit();
                str = str.Trim(new char[] { ',' });
                if (str != "")
                {
                    this._xuli.f_Update_dulieu(string.Concat(new object[] { "delete from ", this._xuli.pSchema, ".", dtoDToaThuocNgoaiVienCT.tableName, " where ", dtoDToaThuocNgoaiVienCT.columnID, " not in (", str, ") and ", dtoDToaThuocNgoaiVienCT.columnIDTOATHUOC, "=", ToaThuoc.pID }));
                }
                pID = ToaThuoc.pID;
            }
            catch
            {
                transaction.Rollback();
                pID = 0M;
            }
            finally
            {
                this._conn.Close();
                this._conn = null;
            }
            return pID;
        }
    }
}

